using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SGGenSet
{
    public partial class Form1 : Form
    {
        private String srcDir = "c:\\";
        private String targetDir = "c:\\temp";
        private String tempDirStr = "temp";
        private FileInfo[] filelist;
        private DirectoryInfo srcDirObj;
        private DirectoryInfo targetDirObj;
        private String origDirStr = "originals";
        private String thumbsDirStr = "thumbs";
        private String imagesDirStr = "images";
        private String cssDirStr = "css";
        private String jsDirStr = "js";
        private DirectoryInfo tempDir;
        private String albumname = "temp";
        private String albumnamelong = "Album 1";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                srcDir =  this.folderBrowserDialog1.SelectedPath;
                this.txtSrcdir.Text = srcDir;
            }
            
        }

        private void btnSelectTargetdir_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                targetDir = this.folderBrowserDialog1.SelectedPath;
                this.txtTargetdir.Text = targetDir;
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            /*srcDirObj = new DirectoryInfo(srcDir);
            filelist = srcDirObj.GetFiles("*.jpg");
            this.listBox1.DataSource = filelist;
            */
            mainThumbView.FolderName = this.srcDir;
            mainThumbView.loadItems();

            // initialize temp dir
            tempDir = new DirectoryInfo(".\\" + tempDirStr);
            tempDir.CreateSubdirectory(this.thumbsDirStr);
            tempDir.CreateSubdirectory(this.imagesDirStr);
        }

        private void btnDeploy_Click(object sender, EventArgs e)
        {
            targetDirObj = new DirectoryInfo(targetDir);
            // create required directory structure in target dir
            targetDirObj.CreateSubdirectory(this.origDirStr);
            targetDirObj.CreateSubdirectory(this.thumbsDirStr);
            targetDirObj.CreateSubdirectory(this.imagesDirStr);
            targetDirObj.CreateSubdirectory(this.cssDirStr);
            targetDirObj.CreateSubdirectory(this.jsDirStr);

            // deploy js files
            copyDirectory(".\\jslib\\" + this.jsDirStr, targetDir + "\\" + this.jsDirStr);

            // deploy css files
            copyDirectory(".\\jslib\\" + this.cssDirStr, targetDir + "\\" + this.cssDirStr);

            // copy original files to $target\originals
            copyDirectory(srcDir, targetDir + "\\" + this.origDirStr);

            // copy thumbs from working dir to target\thumbs
            copyDirectory(".\\" + this.tempDirStr + "\\" + this.thumbsDirStr, targetDir + "\\" + this.thumbsDirStr);

            // copy scaled down images to $target\images
            copyDirectory(".\\" + this.tempDirStr + "\\" + this.imagesDirStr, targetDir + "\\" + this.imagesDirStr);

            // copy the bootstrap html file to target
            File.Copy(".\\" + this.tempDirStr + "\\index.html", targetDir + "\\" + "index.html",true);
        }

        private void copyDirectory(string Src,string Dst){
            String[] Files;

            if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar) 
                Dst+=Path.DirectorySeparatorChar;
            if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
            Files=Directory.GetFileSystemEntries(Src);
            foreach(string Element in Files){
                // Sub directories
                if(Directory.Exists(Element)) 
                    copyDirectory(Element,Dst+Path.GetFileName(Element));
                // Files in directory
                else 
                    File.Copy(Element,Dst+Path.GetFileName(Element),true);
                }
            }

        private void btnCreateThumbs_Click(object sender, EventArgs e)
        {
            foreach (FileInfo fi in this.filelist)
            {
                // create thumb and store in $temp\thumbs

                // create an image object, using the filename we just retrieved
                System.Drawing.Image image = System.Drawing.Image.FromFile(fi.FullName);

                // create the thumbnail image
                System.Drawing.Image thumbnailImage = image.GetThumbnailImage(64, 64, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
                thumbnailImage.Save(".\\temp\\" + this.thumbsDirStr + "\\" + fi.Name);

                // create scale down and store in $temp\images
                System.Drawing.Image scaledImage = fixedSize(image, 640, 480);
                scaledImage.Save(".\\temp\\" + this.imagesDirStr + "\\" + fi.Name);
            }
        }

        ///  <summary>
        /// Required, but not used
        /// </summary>
        /// <returns>true</returns>
        public bool ThumbnailCallback()
        {
            return true;
        }


        private Image fixedSize(Image imgPhoto, int Width, int Height)
        {
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
            {
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width -
                              (sourceWidth * nPercent)) / 2);
            }
            else
            {
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height -
                              (sourceHeight * nPercent)) / 2);
            }

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap bmPhoto = new Bitmap(Width, Height,
                              PixelFormat.Format24bppRgb);
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                             imgPhoto.VerticalResolution);

            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            grPhoto.Clear(Color.Red);
            grPhoto.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;

            grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();
            return bmPhoto;
        }

        private void btnCreateHTML_Click(object sender, EventArgs e)
        {
            albumname = txtAlbumShort.Text;
            albumnamelong = txtAlbumLong.Text;            
            createHTML();
        }

        private void createHTML()
        {
            StreamReader sr = new StreamReader(".\\" + "\\jslib\\header.snap");

            StreamWriter sw = new StreamWriter(".\\" + this.tempDirStr + "\\index.html");
            String ln;
            while ((ln = sr.ReadLine()) != null)
            {
                sw.WriteLine(ln);
            }
            sr.Close();

            sw.WriteLine("<h1>Album : " + albumnamelong + " </h1><br>");
            DirectoryInfo di = new DirectoryInfo(this.srcDir);
            FileInfo[] imagelist = di.GetFiles("*.jpg");

            foreach (FileInfo fi in imagelist)
            {
                String str = "<a href=\"" + this.imagesDirStr + "/" + fi.Name + "\" rel=\"lightbox[" + albumname + "]\"><img src=\"" +
                                this.thumbsDirStr + "/" + fi.Name + "\" alt=\"" + fi.Name + "\" /></a>";

                sw.WriteLine(str);
            }

            sw.WriteLine("</body>");
            sw.WriteLine("</html>");
            sw.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            
        }
    }
}